<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="goodInfo.query" @clear="getGoodsList()">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="add()" >添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格 
      data:显示的数据，是请求来的数据
      label:显示的标题
      prop：对应列表内容的字段名,直接写字段名就ok！
      -->
      <el-table :data="Lists" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="upd_time" label="创建时间">
          <!-- 自定义渲染 用slot-scope接收插槽里面的数据  -->
          <template slot-scope="scope">{{scope.row.upd_time | dataFormate}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeList(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域
      @size-change:每条多少页
      @current-change：当前页
      -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodInfo.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="goodInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 初始化商品列表接口需要传递的参数，pagenum当前页码，pagesize每页显示条数。通过params接收
      goodInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      //   定义接收到的数据
      Lists: [],
      //   总数据条数，用来实现分页
      total: 0
    };
  },
  created() {
    // 获取商品列表数据
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods/", {
        params: this.goodInfo
      });
      console.log(res);
      //   获取到数据，将数据渲染到页面
      // 先进行判断，看是否请求成功
      if (res.meta.status !== 200)
        return this.$message.error("请求好商品列表数据失败");
      this.Lists = res.data.goods;
      this.total = res.data.total;
    },
    // 分页的两个函数 每页显示多少条
    handleSizeChange(newSize) {
      // 进行分页
      this.goodInfo.pagesize = newSize;
      // 分页之后，立重新加载页面数据
      this.getGoodsList();
    },
    // 当前页码
    handleCurrentChange(newNum) {
      this.goodInfo.pagenum = newNum;
      this.getGoodsList();
    },
    // 删除
    async removeList(id) {
      const confirmDel = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmDel !== "confirm") return this.$message.info("已经取消删除");
      const { data: res } = await this.$http.delete("goods/" + id);
      console.log(res);

      if (res.meta.status !== 200) return this.$message.error("删除失败");
      this.getGoodsList();
      this.$message.success("删除商品数据成功");
    },
    add(){
        this.$router.push('/goods/add')
    }
  }
};
</script>
<style lang="less" scoped>
</style>